package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.ImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.UploadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("ordersetting")
@Slf4j
public class OrderSettingController {

    @DubboReference
    OrderSettingService orderSettingService;


    /**
     * @Author: JASON
     * @Date: 14:43 2021/1/9
     * @Parms [multipartFile]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 批量上传预约数量
    */
    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile){
        log.info("预约设置-上传fileName:{},size{}",multipartFile.getOriginalFilename(),multipartFile.getSize());
        String filename = multipartFile.getOriginalFilename();
        if(StringUtils.isEmpty(filename)){
            return new Result(false, "缺少文件名");
        }
        List<List<Object>> lists = null;
        try(InputStream is=multipartFile.getInputStream()){
            //构造workbook
            if(filename.endsWith(".xls")){
                lists= ImportUtil.read2003Excel(is);
            }else {
                lists = ImportUtil.read2007Excel(is);
            }
            List<OrderSetting> orderSettings = ListparseToOrderSettingList(lists);
            log.info("[预约设置-上传]解析成功,result:{}",orderSettings);
            //rpc调用数据入库
            orderSettingService.addAll(orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * @Author: JASON 
     * @Date: 15:48 2021/1/9
     * @Parms [lists]
     * @ReturnType: java.util.List<com.itheima.health.pojo.OrderSetting>
     * @Description: 将List<List<Object>> 转换为 List<OrderSetting>
    */
    private List<OrderSetting> ListparseToOrderSettingList(List<List<Object>> lists) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OrderSetting> orderSettings = new ArrayList<>();
        for (int i = 1; i < lists.size(); i++) {
            List<Object> list = lists.get(i);
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(sdf.parse((String) list.get(0)));
            orderSetting.setNumber(Integer.valueOf(String.valueOf(list.get(1))));
            orderSettings.add(orderSetting);
        }
        return orderSettings;
    }


    /**
     * @Author: JASON
     * @Date: 12:25 2021/1/9
     * @Parms [orderSetting]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 设置预约人数
    */
    @PostMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        log.info("设置预约人数"+orderSetting);
        orderSettingService.editNumberByDate(orderSetting);

        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
    /**
     * @Author: JASON
     * @Date: 10:53 2021/1/9
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询预约数据
    */
    @RequestMapping("findByMonth")
    public Result fingByMonth(Integer year,Integer month){
        log.info("预约设置-查询预约数据year：{},Month:{}",year,month);
        List<OrderSetting> orderSettingList=orderSettingService.findByMonth(year, month);
        log.info("根据年月查询出的OrderSettingList为"+orderSettingList);
        //嵌套 (转换为前端想要的json数据)
        List<Map<String, Object>> list = new ArrayList<>();
        parseVal(orderSettingList, list);
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS,list);
    }

    /**
     * @Author: JASON
     * @Date: 11:07 2021/1/9
     * @Parms [orderSettingList, list]
     * @ReturnType: void
     * @Description: 将orderSettingList转成前端需要数据
    */
    private void parseVal(List<OrderSetting> orderSettingList, List<Map<String, Object>> list) {
        //遍历orderSettingList
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String, Object> map = new HashMap<>();
            //为了防止数据为null 前端不能解析 设置三目运算判断 赋默认值为0
            map.put("number", orderSetting.getNumber()==null?0:orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations()==null?0:orderSetting.getReservations());
            //日期转换获取
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            int date = calendar.get(Calendar.DAY_OF_MONTH);
            map.put("date", date);
            //添加结果
            list.add(map);
        }
    }
}
